// formPackage/pages/houseCompare/houseCompareList/houseCompareList.js
import { queryCompareList, addHouseCompare, queryNewHouseByCondition } from "../../../../service/house_service"
import debounce from "../../../../tools/debounce"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    houseList: [

    ],
    searchWord: "",
    houseNumber: null,
    selectArr: [],
    searchShowList: [],//搜索显示结果,
    searchList: [],//搜索处理列表
    requestData: {
      cityName: "成都市",
      houseStyle: "",
      maxArea: null,
      maxTotalPrice: null,
      maxUnitPrice: null,
      minArea: null,
      minTotalPrice: null,
      minUnitPrice: null,
      page: 0,
      pageSize: 10,
      saleStatus: "",
      subway: "",
      tags: [],
      words: "",
      zone: ""
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      houseNumber: options.houseNumber
    })
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
    this.queryCompareList();
    this.setData({
      selectArr: [],
    })
  },
  //查询对比列表;
  queryCompareList() {
    wx.showLoading();
    addHouseCompare(this.data.houseNumber).then(res => {
      wx.hideLoading();
      console.log("JJLog:对比列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        if (res.data && res.data.length > 0) {
          this.dealHouseList(res.data);
        }
      }
    }).catch(err => {
      console.error("JJLog:addHouseCompare", err);
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
    })
  },
  dealHouseList(list) {
    list.forEach((item) => {
      item.isChoose = false;
    })
    this.setData({
      houseList: list
    })
  },
  //搜索回调
  searchAction(e) {
    // console.log("JJLog:搜索回调:" + JSON.stringify(e));
    this.setData({
      searchWord: e.detail.searchWord,
      'requestData.words': e.detail.searchWord,
    })
    debounce(this.queryNewHouseByCondition);
  },

  //选择房源:
  houseSelectAction(e) {
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    let isChoose = item.isChoose;
    let sArr = this.data.selectArr;
    let arr = this.data.houseList;
    if (!isChoose) {
      //加入到selectArr
      if (sArr.length == 2) {
        wx.showToast({
          title: '最多选择两个房源',
          icon: "none"
        })
        return;
      }
      let obj = {
        houseNumber: item.houseNumber,
      }
      sArr.push(obj);
      this.setData({
        selectArr: sArr
      })

    }
    if (isChoose) {
      //从selectArr中删除:
      let cIndex = 0;
      sArr.forEach((i, idx) => {
        if (i == item.houseNumber) {
          cIndex = idx
        }
      })
      sArr.splice(cIndex, 1);
      this.setData({
        selectArr: sArr
      })
    }


    let changeItem = 'houseList[' + index + '].isChoose';
    this.setData({
      [changeItem]: !isChoose,
    })

  },




  //开始pk:
  startPkAction() {
    if (this.data.selectArr.length != 2) {
      wx.showToast({
        title: '请选择两个PK房源',
        icon: "none"
      })
      return;
    }
    console.log("JJLog:" + JSON.stringify(this.data.selectArr));
    let houseNumber1 = this.data.selectArr[0].houseNumber;
    let houseNumber2 = this.data.selectArr[1].houseNumber;
    wx.navigateTo({
      url: '/formPackage/pages/houseCompare/houseCompareDetail/houseCompareDetail?houseNumber1=' + houseNumber1 + "&houseNumber2=" + houseNumber2,
    })
  },

  //搜索房源:
  queryNewHouseByCondition() {
    wx.showLoading();
    queryNewHouseByCondition(this.data.requestData).then(res => {
      wx.hideLoading();
      console.log("JJLog:搜索结果:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          searchShowList: [],
        })
        if (res.data && res.data.length > 0) {
          if (this.data.searchWord) {
            this.setData({
              searchShowList: res.data
            })
          }
          let searchData = res.data.map((res) => {
            return {
              key: this.data.searchWord,
              name: res.name
            }
          })
          this.setData({
            searchList: searchData
          })

        }
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "搜索失败",
        icon: "none"
      })
      console.error("JJLog:queryNewHouseByCondition", err);
    })
  },


  //搜锁房源点击
  houseSingleTapAction(e) {

    let houseNumber = e.currentTarget.dataset.number;
    this.setData({
      houseNumber: houseNumber,
      searchWord: ""
    })
    this.queryCompareList();
  },






})